
<style>

    #tablesss{
        border: 1px solid #333;
        overflow: hidden;
        /*height: 85vh;*/
    }
    #tablesss > thead > tr > th{
        border: 1px solid #333;
    }
    #tablesss td, #tablesss th{
        border: 1px solid #333;
    }


</style>
<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
    <div class="panel-heading">
        <h2>费用录入</h2>
    </div>

    <div class="form-group">
        <label for="deductions" class="control-label col-xs-12 col-sm-2">{:__('选择服务类型')}:</label>
        <div class="col-xs-12 col-sm-8">
            <select id="deductions" data-rule="required" class="form-control selectpicker">
                <option data-type="type" value="1" {if condition="$row.is_type==1"}selected{/if}>赠送服务</option>
                <option data-type="type" value="2" {if condition="$row.is_type==2"}selected{/if}>内购申请</option>
            </select>

        </div>
    </div>

    <div class="form-group">
        <label for="c-check_code" class="control-label col-xs-12 col-sm-2">{:__('审批单号')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-check_code"  class="form-control" name="row[check_code]" type="text" value="{$row.check_code|htmlentities}" data-tip="">
        </div>
        <a href="javascript:;" name="users" class="btn btn-success" >{:__('查询')}</a>
    </div>

    <div class="form-group">
        <label for="c-iscounts_cost" class="control-label col-xs-12 col-sm-2">{:__('申请优惠金额')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-iscounts_cost" readonly="readonly" class="form-control" name="row[iscounts_cost]" type="text" value="{$row.iscounts_cost|htmlentities}" data-tip="">
        </div>
    </div>

    <div class="form-group">
        <label for="c-other_cost" class="control-label col-xs-12 col-sm-2"><span style="color: red;font-size: 15px">*</span>{:__('第三方服务费')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-other_cost" data-rule="required" class="form-control" name="row[other_cost]" type="text" value="{$row.other_cost|htmlentities}">
        </div>
    </div>

    <div class="form-group" id="custom">
<!--        <label for="c-other_cost" class="control-label col-xs-12 col-sm-1"></label>-->
        <div class="col-xs-12 col-sm-2" style="padding-left: 70px;">
            <input id="c-custom_name"  class="form-control" name="row[custom_name]" type="text" value="{$row.new_str|htmlentities}" placeholder="自定义名称">
        </div>
        <div class="col-xs-12 col-sm-8">
            <input id="c-custom_price"  class="form-control" name="row[custom_price]" type="text" value="{$row.new_price|htmlentities}" placeholder="自定义金额：输入-符号的数值代表扣除.例子:-50，正常输入数值代表加.例子:50">
        </div>

        <a href="javascript:;" name="del" class="btn btn-danger" >{:__('删除')}</a>
    </div>

    <input id="is_custom" type="hidden" value="">

    <div class="form-group" id="customs">
        <label for="c-other_cost" class="control-label col-xs-12 col-sm-2">
            <a href="javascript:;" name="customs" class="btn btn-success" >{:__('自定义金额')}</a>
        </label>

    </div>


    <div class="panel-heading">
        <h2>抵扣会员服务</h2>
    </div>

    <div class="form-group">
        <label for="deduction" class="control-label col-xs-12 col-sm-2">{:__('是否抵扣会员服务')}:</label>
        <div class="col-xs-12 col-sm-8">
            <select id="deduction" data-rule="required" class="form-control selectpicker" name="row[deduction]">
                <option data-type="type" value="1" {if condition="$row.is_types==1"}selected{/if}>否</option>
                <option data-type="type" value="2" {if condition="$row.is_types==2"}selected{/if}>是</option>
            </select>

        </div>
    </div>

    <div class="form-group">
        <label for="c-vip_deduction" class="control-label col-xs-12 col-sm-2">{:__('会员抵扣服务金额')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-vip_deduction"  class="form-control" readonly="readonly" name="row[vip_deduction]" type="text" value="{$row.vip_deduction|htmlentities}">
        </div>
    </div>

    <div class="col-xs-12 col-sm-10">
        <table id="tablesss" class="table table-striped">
            <thead>
            <tr>
                <!--<th width="15%"><input id="all" data-index="0" name="btSelectItem" type="checkbox"></th>-->
                <th width="10%"><input type="checkbox" data-check-target=".id-checkbox" /></th>
                <th width="10%">序号</th>
                <th width="16%">会员服务名称</th>
                <th width="16%">数量</th>
                <th width="16%">单位</th>
                <th width="16%">单价</th>
                <th width="16%">总价</th>
            </tr>
            </thead>
            <tbody>



            {foreach $vip as $index=>$vo}
            <tr>
                <!--<td class="bs-checkbox "><input data-index="0" name="btSelectItem" type="checkbox"></td>-->
                <td class="bs-checkbox "><input type="checkbox" name="ids" value="{$vo.serve_id},{$vo.total}" class="id-checkbox" {if condition="in_array($vo.serve_id,$for)"}checked{/if} /></td>
                <td>{$index+1}</td>
                <td>{$vo.serve_name}</td>
                <td>{$vo.count}</td>
                <td>{$vo.units}</td>
                <td>{$vo.price}</td>
                <td>{$vo.total}</td>
            </tr>
            {/foreach}


            </tbody>
        </table>
    </div>


</form>

<div class="form-group layer-footer">
    <label class="control-label col-xs-12 col-sm-2"></label>
    <div class="col-xs-12 col-sm-8">
        <!--<button id="news" type="submit1" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>-->
        <button id="news" type="submit" class="btn btn-success btn-embossed btn-close">小计</button>
        <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
    </div>
</div>

<script src="/assets/img/jquery-3.4.1.min.js"></script>
<script>
    var prices = parseFloat($("#c-vip_deduction").val());
    var hi = {$hi};
    $('#tablesss').hide();
    if (hi == 1){
        $('#customs').hide();
        $('#custom').show();
        $("#is_custom").val(1);
    }else {
        $('#custom').hide();
        $("#is_custom").val(0);
    }


    var is_deduction = $("#deduction").val();
    if (is_deduction == 2){
        $('#tablesss').show();
    }else {
        $('#tablesss').hide();
    }


    $(document).on("change", "#deduction", function(){
        //变更后的回调事件
        let type = $("#deduction").val();
        if (type == 2) {
            $('#tablesss').show();
            $("#c-vip_deduction").val({$row.vip_deduction});
        }else {
            $('#tablesss').hide();
            $("#c-vip_deduction").val('0');
        }

    });

    $('.id-checkbox').change(function(){
        var arr = this.value.split(',');
        prices = parseFloat($("#c-vip_deduction").val());
        if(this.checked){
            prices = prices + parseFloat(arr[1]);
        }else{
            prices = prices - parseFloat(arr[1]);
        }
        $("#c-vip_deduction").val(prices);

    })

    $(':checkbox[data-check-target]').click(function () {
        var target = $(this).attr('data-check-target');

        if ($(this).prop('checked')) {
            $(target).prop('checked', true);
            var chk_value =[];//定义一个数组
            var price = 0;
            $(":input[name='ids']:checked").each(function(){
                arr = this.value.split(',');
                price += parseFloat(arr[1]);
                chk_value.push(arr[0]);//将选中的值添加到数组chk_value中
            })

            //var groups = chk_value.join(",");

            $("#c-vip_deduction").val(price);


        } else {
            $(target).prop('checked', false);
            $("#c-vip_deduction").val('0');
        }
    });




    $('#news').click(function(){
        let iscounts_cost = $("#c-iscounts_cost").val();
        let check_code = $("#c-check_code").val();
        let other_cost = $("#c-other_cost").val();
        let vip_deduction = $("#c-vip_deduction").val();
        let is_custom = $("#is_custom").val();
        let custom_name = $("#c-custom_name").val();
        let custom_price = $("#c-custom_price").val();

        let type =  $("#deductions").val();
        let is_types =  $("#deduction").val();

        let id = {$row.id};

        var chk_value =[];//定义一个数组

        $(":input[name='ids']:checked").each(function(){
            arr = this.value.split(',');
            chk_value.push(arr[0]);//将选中的值添加到数组chk_value中
        })

        var groups = chk_value.join(",");

        //console.log(groups);

        urls = 'financing/accepts/fors?id='+{$row.id}+'&iscounts_cost='+iscounts_cost+'&check_code='+check_code+'&other_cost='+other_cost+'&vip_deduction='+vip_deduction+'&vip_id='+groups+'&type='+type+'&is_custom='+is_custom+'&custom_name='+custom_name+'&custom_price='+custom_price+'&is_types='+is_types;

        $.ajax({
            url:"financing/accepts/fors",    //请求的url地址
            dataType:"json",   //返回格式为json
            async:true,//请求是否异步，默认为异步，这也是ajax重要特性
            data:{"id":id,"iscounts_cost":iscounts_cost,"check_code":check_code,"other_cost":other_cost,"is_custom":is_custom,"custom_name":custom_name,"custom_price":custom_price},//参数值
            type:"POST",//请求方式
            success:function(data){
                //请求成功时处理
                if (data.code == 0) {
                    Toastr.error(data.msg);
                }else {
                    Fast.api.open(urls, __('小计'), {});
                }
            },
        });


        //url = 'financing/accepts/fors?id='+{$row.id}+'&iscounts_cost='+iscounts_cost+'&check_code='+check_code+'&other_cost='+other_cost+'&reality_cost='+reality_cost;

        //Fast.api.open(url, __('小计'), {});
    })


    $("a[name='users']").click(function(){
        let type =  $("#deductions").val();
        let code =  $("#c-check_code").val();
        let id = {$row.id};
        //console.log(type);

        $.ajax({
            url:"financing/accepts/code",    //请求的url地址
            dataType:"json",   //返回格式为json
            async:true,//请求是否异步，默认为异步，这也是ajax重要特性
            data:{"id":id,"type":type,"code":code},    //参数值
            type:"POST",   //请求方式
            success:function(data){
                //请求成功时处理
                if (data.code == 0) {
                    //console.log(data);
                    Toastr.error(data.msg);
                    $("#c-iscounts_cost").attr("value",0);
                }else {
                    //parent.Toastr.success(data.msg);
                    $("#c-iscounts_cost").attr("value",data.data);
                }
            },
        });

    });

    $("a[name='customs']").click(function () {
        //console.log(8888);
        $('#custom').show();
        $('#customs').hide();
        $("#is_custom").val(1);
    });

    $("a[name='del']").click(function () {
        //console.log(8888);
        $('#custom').hide();
        $('#customs').show();
        $("#is_custom").val(0);
    });


</script>
